home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10891 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  84 lines

  1. Path: news.delphi.com!usenet
  2. From: JGUILLORY@delphi.com
  3. Newsgroups: comp.lang.c++
  4. Subject: passing struct to constructor  -  temp.txt [1/1]
  5. Date: 9 Mar 1996 17:18:25 GMT
  6. Organization: Delphi Internet Services Corporation
  7. Message-ID: <4hseh1$rkk@news2.delphi.com>
  8. References: <4eqgq3$5g1@geraldo.cc.utexas.edu>
  9. NNTP-Posting-Host: bos1g.delphi.com
  10. X-NewsReader: Rainbow V 1.20.0 for Delphi
  11.  
  12.  
  13. Quoting wharris from a message in comp.lang.c++
  14.  wh>The compiler error I'm getting is "VIN is not a member of struct1".  The
  15.  wh>code has been severely minimized, however this should have the bare
  16.  wh>essentials required to solve the problem.
  17.  wh>class CarData
  18.  wh>{
  19.  wh>private :
  20.  wh>char VIN[7];
  21.  wh>public :
  22.  wh>CarData(struct astruct1 CarInf);
  23.  wh>};
  24.  wh>//********************************************************
  25.  wh>CarData::CarData(struct astruct1 CarInf)
  26.  wh>{
  27.  wh>strcpy(VIN,CarInf.VIN);
  28.  wh>}
  29.  wh>struct astruct1
  30.  wh>    {
  31.  wh>    char VIN[7];
  32.  wh>    }CarInfo;
  33.  wh>void main()
  34.  wh>{
  35.  wh>CarData CarConst1(struct astruct1 CarInfo);
  36.  wh>}
  37.  
  38.   The key is 'private:'   Using private restricts access to VIN
  39.   by any other part of your program except from within the object
  40.   (CarData).  Eg. you could create another function inside of CarData
  41.   eg:
  42.  
  43. void CarData::Test(void) {
  44.      CarConst1(VIN);
  45. };
  46.  
  47. But, that presents 2 problems, 1> Your constructor should have already
  48. been initialized by the time your program has a chance to call
  49. CarData::Test() .... 2> Why are you naming your constructor CarConst1?
  50. Constructors for objects should always be named the same as their
  51. object, and Destructors the same, except a ~ in front of the name.
  52. With that naming scheme, the constructor gets called as soon as you
  53. use new or allocate memory for the object.  If you need more than
  54. one set of paramaters for the constructor, create as many constructors
  55. as you need, this technique is I believe called "Overloading")
  56. Eg:
  57.  
  58. class Car {
  59. private:
  60.          int data;
  61.  
  62. public:
  63.          Car(int a) { data = a; }
  64.          Car() { data = 0; }
  65. };
  66.  
  67.  
  68. Thus:
  69.  
  70. void main(void) {
  71. Car Mine(),Yours(2);
  72.  
  73. }
  74.  
  75.                 Mine.data would equal 0,
  76.                 Yours.data would equal 2.
  77.  
  78. `[0;1;34mJohn H. Guillory
  79. `[33mLoving the Lord in '96      `[34mJGuillory@Delphi.Com                                                         
  80.  
  81. I have read and understood the above X______________.
  82.  
  83.  
  84.